/*
 * StockControlView.java
 */

package stockcontrol;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.beans.Beans;
import javax.swing.Icon;
import javax.swing.JComponent;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * The application's main frame.
 */
public class StockControlView extends FrameView {

    public StockControlView(SingleFrameApplication app){
        super(app);
        try{     
            initComponents();
            if (!Beans.isDesignTime()) {
                entityManager.getTransaction().begin();
            }
            InicioForm inicioForm = new InicioForm(entityManager);
            panelPrincipal.add(inicioForm, 1);
        
        } catch ( Exception e ){
            JOptionPane.showMessageDialog(null,
                "No se pudo conectar a la Base de Datos",
                "Error de Conexion",
                JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }


            
       
        


/*
        JPanel foregroundPanel = new JPanel(new BorderLayout(10, 10));
        foregroundPanel.setBorder(
                BorderFactory.createEmptyBorder(10,10,10,10));
        foregroundPanel.setOpaque(false);

        panelPrincipal.add(wrapInBackgroundImage(foregroundPanel,
                new ImageIcon("D:\\NetBeansProjects\\taller-protegido\\src\\stockcontrol\\resources\\bg.jpg") ));
*/
    }
    
    private static final GridBagConstraints gbc;
    static {
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.NORTHWEST;
    }
    /**
     * Wraps a Swing JComponent in a background image. Simply invokes the overloded
     * variant with Top/Leading alignment for background image.
     *
     * @param component - to wrap in the a background image
     * @param backgroundIcon - the background image (Icon)
     * @return the wrapping JPanel
     */
    public static JPanel wrapInBackgroundImage(JComponent component,
            Icon backgroundIcon) {
        return wrapInBackgroundImage(
                component,
                backgroundIcon,
                JLabel.TOP,
                JLabel.LEADING);
    }

    /**
     * Wraps a Swing JComponent in a background image. The vertical and horizontal
     * alignment of background image can be specified using the alignment
     * contants from JLabel.
     *
     * @param component - to wrap in the a background image
     * @param backgroundIcon - the background image (Icon)
     * @param verticalAlignment - vertical alignment. See contants in JLabel.
     * @param horizontalAlignment - horizontal alignment. See contants in JLabel.
     * @return the wrapping JPanel
     */
    public static JPanel wrapInBackgroundImage(JComponent component,
            Icon backgroundIcon,
            int verticalAlignment,
            int horizontalAlignment) {

        // make the passed in swing component transparent
        component.setOpaque(false);

        // create wrapper JPanel
        JPanel backgroundPanel = new JPanel(new GridBagLayout());

        // add the passed in swing component first to ensure that it is in front
        backgroundPanel.add(component, gbc);

        // create a label to paint the background image
        JLabel backgroundImage = new JLabel(backgroundIcon);

        // set minimum and preferred sizes so that the size of the image
        // does not affect the layout size
        backgroundImage.setPreferredSize(new Dimension(1,1));
        backgroundImage.setMinimumSize(new Dimension(1,1));

        // align the image as specified.
        backgroundImage.setVerticalAlignment(verticalAlignment);
        backgroundImage.setHorizontalAlignment(horizontalAlignment);

        // add the background label
        backgroundPanel.add(backgroundImage, gbc);

        // return the wrapper
        return backgroundPanel;
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = StockControlApp.getApplication().getMainFrame();
            aboutBox = new StockControlAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        StockControlApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        buttonBar = new com.l2fprod.common.swing.JButtonBar();
        buttonInicio = new javax.swing.JToggleButton();
        buttonCompras = new javax.swing.JToggleButton();
        buttonVentas = new javax.swing.JToggleButton();
        buttonBalances = new javax.swing.JToggleButton();
        buttonEstadisticas = new javax.swing.JToggleButton();
        panelPrincipal = new org.jdesktop.swingx.JXTitledPanel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("databasePU").createEntityManager();

        mainPanel.setName("mainPanel"); // NOI18N

        buttonBar.setName("buttonBar"); // NOI18N
        com.l2fprod.common.swing.PercentLayout percentLayout1 = new com.l2fprod.common.swing.PercentLayout();
        percentLayout1.setGap(2);
        percentLayout1.setOrientation(1);
        buttonBar.setLayout(percentLayout1);

        buttonGroup1.add(buttonInicio);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(stockcontrol.StockControlApp.class).getContext().getResourceMap(StockControlView.class);
        buttonInicio.setIcon(resourceMap.getIcon("buttonInicio.icon")); // NOI18N
        buttonInicio.setText(resourceMap.getString("buttonInicio.text")); // NOI18N
        buttonInicio.setName("buttonInicio"); // NOI18N
        buttonInicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonInicioActionPerformed(evt);
            }
        });
        buttonBar.add(buttonInicio);

        buttonGroup1.add(buttonCompras);
        buttonCompras.setIcon(resourceMap.getIcon("buttonCompras.icon")); // NOI18N
        buttonCompras.setText(resourceMap.getString("buttonCompras.text")); // NOI18N
        buttonCompras.setName("buttonCompras"); // NOI18N
        buttonCompras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonComprasActionPerformed(evt);
            }
        });
        buttonBar.add(buttonCompras);

        buttonGroup1.add(buttonVentas);
        buttonVentas.setIcon(resourceMap.getIcon("buttonVentas.icon")); // NOI18N
        buttonVentas.setText(resourceMap.getString("buttonVentas.text")); // NOI18N
        buttonVentas.setName("buttonVentas"); // NOI18N
        buttonVentas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonVentasActionPerformed(evt);
            }
        });
        buttonBar.add(buttonVentas);

        buttonGroup1.add(buttonBalances);
        buttonBalances.setIcon(resourceMap.getIcon("buttonBalances.icon")); // NOI18N
        buttonBalances.setText(resourceMap.getString("buttonBalances.text")); // NOI18N
        buttonBalances.setName("buttonBalances"); // NOI18N
        buttonBalances.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBalancesActionPerformed(evt);
            }
        });
        buttonBar.add(buttonBalances);

        buttonGroup1.add(buttonEstadisticas);
        buttonEstadisticas.setIcon(resourceMap.getIcon("buttonEstadisticas.icon")); // NOI18N
        buttonEstadisticas.setText(resourceMap.getString("buttonEstadisticas.text")); // NOI18N
        buttonEstadisticas.setName("buttonEstadisticas"); // NOI18N
        buttonEstadisticas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEstadisticasActionPerformed(evt);
            }
        });
        buttonBar.add(buttonEstadisticas);

        panelPrincipal.setTitle(resourceMap.getString("panelPrincipal.title")); // NOI18N
        panelPrincipal.setTitleFont(resourceMap.getFont("panelPrincipal.titleFont")); // NOI18N
        panelPrincipal.setName("panelPrincipal"); // NOI18N

        javax.swing.GroupLayout panelPrincipalLayout = new javax.swing.GroupLayout(panelPrincipal.getContentContainer());
        panelPrincipal.getContentContainer().setLayout(panelPrincipalLayout);
        panelPrincipalLayout.setHorizontalGroup(
            panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 797, Short.MAX_VALUE)
        );
        panelPrincipalLayout.setVerticalGroup(
            panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 478, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(buttonBar, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panelPrincipal, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonBar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 507, Short.MAX_VALUE))
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(stockcontrol.StockControlApp.class).getContext().getActionMap(StockControlView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setText(resourceMap.getString("aboutMenuItem.text")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonVentasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonVentasActionPerformed
        // TODO add your handling code here:
        VentaForm ventaForm = new VentaForm(entityManager);
        panelPrincipal.setTitle("Ventas");
        panelPrincipal.remove(1);
        panelPrincipal.add(ventaForm, 1);
    }//GEN-LAST:event_buttonVentasActionPerformed

    private void buttonComprasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonComprasActionPerformed
        // TODO add your handling code here:
        CompraForm compraForm = new CompraForm(entityManager);
        panelPrincipal.setTitle("Compras");
        panelPrincipal.remove(1);
        panelPrincipal.add(compraForm, 1);
    }//GEN-LAST:event_buttonComprasActionPerformed

    private void buttonBalancesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBalancesActionPerformed
        // TODO add your handling code here:
        BalanceForm balanceForm = new BalanceForm(entityManager);
        panelPrincipal.setTitle("Balances");
        panelPrincipal.remove(1);
        panelPrincipal.add(balanceForm, 1);
    }//GEN-LAST:event_buttonBalancesActionPerformed

    private void buttonInicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonInicioActionPerformed
        // TODO add your handling code here:
        InicioForm inicioForm = new InicioForm(entityManager);
        panelPrincipal.setTitle("Inicio");
        panelPrincipal.remove(1);
        panelPrincipal.add(inicioForm, 1);
    }//GEN-LAST:event_buttonInicioActionPerformed

    private void buttonEstadisticasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEstadisticasActionPerformed
        // TODO add your handling code here:
        EstadisticasForm estadisticasForm = new EstadisticasForm(entityManager);
        panelPrincipal.setTitle("Estadisticas");
        panelPrincipal.remove(1);
        panelPrincipal.add(estadisticasForm, 1);
    }//GEN-LAST:event_buttonEstadisticasActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton buttonBalances;
    private com.l2fprod.common.swing.JButtonBar buttonBar;
    private javax.swing.JToggleButton buttonCompras;
    private javax.swing.JToggleButton buttonEstadisticas;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JToggleButton buttonInicio;
    private javax.swing.JToggleButton buttonVentas;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private org.jdesktop.swingx.JXTitledPanel panelPrincipal;
    // End of variables declaration//GEN-END:variables

    private JDialog aboutBox;
}
